package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.common.enums.GridRecordStatusEnum;
import com.econ.powercloud.ems.dal.entity.EmsGridRecordStep;
import com.econ.powercloud.ems.dal.mapper.EmsGridRecordStepMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class EmsGridRecordStepDAO extends ServiceImpl<EmsGridRecordStepMapper, EmsGridRecordStep> {

    private final String LIMIT_ONE = "limit 1";

    /**
     * 查找上一条停止移峰填谷的记录
     * @param energyStationId 储能站id
     * @param dit 点号/装置id
     * @return
     */
    public EmsGridRecordStep selectLastStopPeakShift(String energyStationId, String dit) {
        LambdaQueryWrapper<EmsGridRecordStep> wrapper = Wrappers.<EmsGridRecordStep>lambdaQuery()
                .eq(EmsGridRecordStep::getEnergyStationId, energyStationId)
                .eq(EmsGridRecordStep::getDit, dit)
                .isNotNull(EmsGridRecordStep::getStopPeakShiftId)
                .orderByDesc(EmsGridRecordStep::getEndTime).last(LIMIT_ONE);
        return this.baseMapper.selectOne(wrapper);
    }

    public List<EmsGridRecordStep> selectByRecordNodeId(Long recordNodeId) {
        LambdaQueryWrapper<EmsGridRecordStep> wrapper = Wrappers.<EmsGridRecordStep>lambdaQuery()
                .eq(EmsGridRecordStep::getRecordNodeId, recordNodeId);
        return this.baseMapper.selectList(wrapper);
    }

    public List<EmsGridRecordStep> selectByRecordNodeIds(List<Long> recordNodeIds) {
        LambdaQueryWrapper<EmsGridRecordStep> wrapper = Wrappers.<EmsGridRecordStep>lambdaQuery()
                .in(EmsGridRecordStep::getRecordNodeId, recordNodeIds);
        return this.baseMapper.selectList(wrapper);
    }

    public EmsGridRecordStep selectByMsgId(Integer msgId) {
        LambdaQueryWrapper<EmsGridRecordStep> wrapper = Wrappers.<EmsGridRecordStep>lambdaQuery().eq(EmsGridRecordStep::getSyncMsgId, msgId);
        return this.baseMapper.selectOne(wrapper);
    }

    public List<EmsGridRecordStep> selectByRecordNodeIdAndOrderNum(Long recordNodeId, Integer orderNum) {
        LambdaQueryWrapper<EmsGridRecordStep> wrapper = Wrappers.<EmsGridRecordStep>lambdaQuery()
                .eq(EmsGridRecordStep::getRecordNodeId, recordNodeId).eq(EmsGridRecordStep::getOrderNum, orderNum);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * 查询某模块下进行中的节点记录
     * @param moduleRecordId
     * @return
     */
    public EmsGridRecordStep selectByModuleIdAndInProgress(Long moduleRecordId, List<Integer> statusList) {
        LambdaQueryWrapper<EmsGridRecordStep> wrapper = Wrappers.<EmsGridRecordStep>lambdaQuery()
                .eq(EmsGridRecordStep::getRecordNodeId, moduleRecordId).in(EmsGridRecordStep::getStatus, statusList);
        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 获取某节点下最新的步骤记录
     * @param recordNodeId 节点记录id
     * @return 步骤记录
     */
    public EmsGridRecordStep selectLatestByNodeId(Long recordNodeId) {
        LambdaQueryWrapper<EmsGridRecordStep> wrapper = Wrappers.<EmsGridRecordStep>lambdaQuery()
                .eq(EmsGridRecordStep::getRecordNodeId, recordNodeId).orderByDesc(EmsGridRecordStep::getId).last(LIMIT_ONE);
        return this.baseMapper.selectOne(wrapper);
    }

    public List<EmsGridRecordStep> findRunning(){
        LambdaQueryWrapper<EmsGridRecordStep> wrapper = Wrappers.lambdaQuery();
        wrapper.in(EmsGridRecordStep::getStatus,GridRecordStatusEnum.getRunningState().stream().map(GridRecordStatusEnum::getCode).collect(Collectors.toList()));
        wrapper.isNotNull(EmsGridRecordStep::getSyncMsgId);
        Page<EmsGridRecordStep> page = new Page<>(1, 10000);
        page = this.baseMapper.selectPage(page, wrapper);
        return page.getRecords();
    }
}
